<template>
  <div>
      <a id="new" @click="handleAdd">修改</a>
      <a-modal v-model="visible" title="修改班级信息" ok-text="确认" cancel-text="取消"  @ok="handleSubmit">
        <a-form :form="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
              <a-form-item label="班号" >
                  <a-input v-decorator="['newId', { rules: [{ required: true , message: '请输入新班号'}] }]"/>
              </a-form-item>
              <a-form-item label="班名" >
                  <a-input v-decorator="['newName', { rules: [{ required: true , message: '请输入新班名'}] }]"/>
              </a-form-item>
          </a-form>
          </a-modal>
  </div>
</template>

<script>
  import { request } from "../../../api/index";
  export default {
    name: 'classUpdate',
    data () {
      return {
        form: this.$form.createForm(this),
        newVal:{
            newId:'',
            newName:''
        },
        visible : false,
      }
    },
    props:['obj'],
    methods: {
      handleSubmit (e) {
        // console.log("aaaa"+this.$store.state)
        this.visible = false;
        e.preventDefault()
        this.form.validateFields((err, value) => {
          if (!err) {
            this.newVal = value;
            // console.log(this.obj.class_id,this.obj.class_name);
            // console.log(this.newVal.newId,this.newVal.newName);
            request({
                url: "/class/class_rename",
                method: "post",
                data: {
                    class_id: this.obj.class_id,
                    class_id_new: this.newVal.newId,
                    class_name_new: this.newVal.newName,
                },
            })
                .then((res) => {
                    // console.log(res)
                    if (res.data.message == "success"){
                        alert("修改成功")
                        this.$emit('refresh')
                    }
                    else{
                        alert("修改失败")
                    }
                })
                .catch((err) => {
                    // console.log(err)
                });
          }
        })
      },
      handleAdd() {
        this.visible = true;
        // console.log(this.obj)
        setTimeout(()=>{
          this.form.setFieldsValue({
          newId:this.obj.class_id,
          newName:this.obj.class_name,
        })
        },50)
      },
    }
  }
</script>
<style scoped>
  #new{
  float: left;
}
</style>